package com.fc.test.controller.gen;

import com.fc.test.common.exception.DataException;
import com.fc.test.common.file.FileUtils;
import com.fc.test.model.auto.Policy;
import com.fc.test.model.auto.TsysDatas;
import com.fc.test.model.auto.TsysFile;
import com.fc.test.util.StringUtils;
import com.fc.test.vo.HhbFileVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.HhbFile;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.HhbFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = "红黑榜文件管理")
@Controller
@RequestMapping("/HhbFileController")
@ApiIgnore
public class HhbFileController extends BaseController {

    private String prefix = "gen/hhbFile";
    @Autowired
    private HhbFileService hhbFileService;

    /**
     * 分页跳转
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:hhbFile:view")
    public String view(ModelMap model) {
        String str = "红黑榜文件管理";
        setTitle(model, new TitleVo("列表", str + "管理", true, "欢迎进入" + str + "页面", true, false));
        return prefix + "/list";
    }

    /**
     * 分页查询
     */
    //@Log(title = "红黑榜文件管理集合查询", action = "111")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/list")
    @RequiresPermissions("gen:hhbFile:list")
    @ResponseBody
    public Object list(Tablepar tablepar, String searchText) {
        PageInfo<HhbFileVo> page = hhbFileService.list2(tablepar, searchText);
        TableSplitResult<HhbFileVo> result = new TableSplitResult<>(page.getPageNum(), page.getTotal(), page.getList());
        return result;
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }


    /**
     * 文件上传
     * @param file
     * @return
     * @author fuce
     * @Date 2019年11月20日 下午10:18:49
     */

    /**
     * 文件添加
     *
     * @param file
     * @return
     */
    //@Log(title = "红黑榜文件管理新增", action = "111")
    @ApiOperation(value = "文件添加", notes = "文件添加")
    @PostMapping("/add")
    @RequiresPermissions("gen:hhbFile:add")
    @ResponseBody
    public AjaxResult add(HhbFile hhbFile,TsysFile file, String dataId) {
        if(StringUtils.isNotEmpty(dataId)) {
            int a = hhbFileService.insertSelective(hhbFile, file, dataId);
            if (a > 0) {
                return success();
            }
        }


        return error("请上传文件");
    }



    /**
     * 删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "红黑榜文件管理删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/remove")
    @RequiresPermissions("gen:hhbFile:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = hhbFileService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 检查Name
     *
     * @param tsysUser
     * @return
     */
    @ApiOperation(value = "检查Name唯一", notes = "检查Name唯一")
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public int checkNameUnique(TsysFile tsysFile) {
        int b = hhbFileService.checkNameUnique(tsysFile.getFileName());
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("HhbFile", hhbFileService.selectById(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "红黑榜文件管理修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:hhbFile:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HhbFile record,TsysFile tsysFile,String dataId) {
        return toAjax(hhbFileService.update(record,tsysFile,dataId));
    }

    /**
     * 展示文件跳转页面
     */
    @ApiOperation(value = "展示文件跳转页面", notes = "展示文件跳转页面")
    @GetMapping("/viewfile/{id}")
    public String viewfile(@PathVariable("id") String id,ModelMap mmap){
        mmap.put("tsysDatas",sysFileDatasService.queryfileID(id));
        return prefix + "/viewfile";
    }




    /**
     * 根据主键查询
     *
     * @param id
     * @param mmap
     * @return
     */
    @ApiOperation(value = "根据id查询唯一", notes = "根据id查询唯一")
    @PostMapping("/get/{id}")
    public HhbFile edit(@PathVariable("id") String id) {
        return hhbFileService.selectByPrimaryKey(id);
    }

    /**
     * 修改保存
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @PostMapping("/status")
    @ResponseBody
    public AjaxResult editStatus(int status,String id )throws DataException
    {
        HhbFile record = new HhbFile();
        record.setId(id);
        record.setStatus(status);
        return toAjax(hhbFileService.updateByPrimaryKeySelective2(record));
    }
}
